package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.ReviewDao;
import com.fduss.artStore.entity.Review;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class ReviewDaoImpl implements ReviewDao {
    private JdbcUtil util;

    public ReviewDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public Review add(Review review) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`review`" +
                " (`ReviewId`, `ArtWorkId`, `CustomerId`, `ReviewDate`, `Rating`, `Comment`)" +
                " VALUES (?, ?, ?, ?, ?, ?)";

        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, review.getReviewId());
            pst.setInt(2, review.getArtWorkId());
            pst.setInt(3, review.getCustomerId());
            pst.setTimestamp(4, review.getReviewDate());
            pst.setInt(5, review.getRating());
            pst.setString(6, review.getComment());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? review : null;
    }

    @Override
    public void delete(int reviewId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`review` WHERE `ReviewID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, reviewId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public void deleteByArtwork(int artworkId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`review` WHERE `ArtworkID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, artworkId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public List<Review> getByArtWork(int artworkId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`review` WHERE `ArtworkID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Review> review = new ArrayList<>();

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, artworkId);
            rs = pst.executeQuery();

            while (rs.next()){
                review.add(buildReview(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return review;
    }

    private Review buildReview(ResultSet rs) throws SQLException {
        Review review = new Review();
        review.setArtWorkId(rs.getInt("ArtworkID"));
        review.setComment(rs.getString("Comment"));
        review.setCustomerId(rs.getInt("CustomerID"));
        review.setRating(rs.getInt("Rating"));
        review.setReviewDate(rs.getTimestamp("ReviewDate"));
        review.setReviewId(rs.getInt("ReviewID"));
        return review;
    }
}
